﻿using Gonte.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using System.IO;
using System;

namespace MyUtilitiesTest
{
    /// <summary>
    ///This is a test class for XmlTraversalTest and is intended
    ///to contain all XmlTraversalTest Unit Tests
    ///</summary>
    [TestClass()]
    public class XmlTraversalTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        /// Tests a traversal with null reader
        /// </summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TraverseNullReaderTest()
        {
            XmlTraversal traversal = new XmlTraversal();
            traversal.Traverse();
        }

        [TestMethod()]
        public void TraverseTextNodeTest()
        {
            String xml =
                @"<XmlAdapterTestObject name=""myName"" class=""myClass"" number=""1234567"">
                    MyInnerObject
                </XmlAdapterTestObject>";

            using (StringReader stringReader = new StringReader(xml))
            {
                using (XmlReader xmlReader = new XmlTextReader(stringReader))
                {
                    XmlTraversal traversal = new XmlTraversal();
                    traversal.Reader = xmlReader;
                    traversal.Traverse();
                }
            }
        }

        [TestMethod()]
        public void TraverseSingleInnerObjectNodeTest()
        {
            String xml =
                @"<XmlAdapterTestObject name=""myName"" class=""myClass"" number=""1234567"">
                    <InnerObject>MyInnerObject</InnerObject>
                </XmlAdapterTestObject>";

            using (StringReader stringReader = new StringReader(xml))
            {
                using (XmlReader xmlReader = new XmlTextReader(stringReader))
                {
                    XmlTraversal traversal = new XmlTraversal();
                    traversal.Reader = xmlReader;
                    traversal.Traverse();
                }
            }
        }

        [TestMethod()]
        public void TraverseDifferentInnerObjectsNodeTest()
        {
            String xml =
                @"<XmlAdapterTestObject name=""myName"" class=""myClass"" number=""1234567"">
                    <InnerObject>MyInnerObject</InnerObject>
                    <InnerObjectWithProperties prop1=""prop1"" prop2=""prop2""></InnerObjectWithProperties>
                </XmlAdapterTestObject>";

            using (StringReader stringReader = new StringReader(xml))
            {
                using (XmlReader xmlReader = new XmlTextReader(stringReader))
                {
                    XmlTraversal traversal = new XmlTraversal();
                    traversal.Reader = xmlReader;
                    traversal.Traverse();
                    int level = traversal.Level;
                }
            }
        }

        [TestMethod()]
        public void TraverseCollectionStringsNodeTest()
        {
            String xml =
                @"<XmlAdapterTestObject name=""myName"" class=""myClass"" number=""1234567"">
                    <InnerObject>MyInnerObject</InnerObject>
                    <InnerObjectWithProperties prop1=""prop1"" prop2=""prop2""></InnerObjectWithProperties>
                    <Names>
                        <Name>Sarah</Name>
                        <Name>Yana</Name>
                        <Name>Mark</Name>
                        <Name>Jorge</Name>
                    </Names>
                </XmlAdapterTestObject>";

            using (StringReader stringReader = new StringReader(xml))
            {
                using (XmlReader xmlReader = new XmlTextReader(stringReader))
                {
                    XmlTraversal traversal = new XmlTraversal();
                    traversal.Reader = xmlReader;
                    traversal.Traverse();
                }
            }
        }

        [TestMethod()]
        public void TraverseDifferentCollectionsNodeTest()
        {
            String xml =
                @"<XmlAdapterTestObject name=""myName"" class=""myClass"" number=""1234567"">
                    <InnerObject>MyInnerObject</InnerObject>
                    <InnerObjectWithProperties prop1=""prop1"" prop2=""prop2""></InnerObjectWithProperties>
                    <Names>
                        <Name>Sarah</Name>
                        <Name>Yana</Name>
                        <Name>Mark</Name>
                        <Name>Jorge</Name>
                    </Names>
                    <SampleObjects>
                        <SampleObject name=""name1"" data=""data1""/>
                        <SampleObject name=""name2"" data=""data2""/>
                        <SampleObject name=""name3"" data=""data3""/>
                        <SampleObject name=""name4"" data=""data4""/>
                        <SampleObject name=""name5"" data=""data5""/>
                    </SampleObjects>
                </XmlAdapterTestObject>";

            using (StringReader stringReader = new StringReader(xml))
            {
                using (XmlReader xmlReader = new XmlTextReader(stringReader))
                {
                    XmlTraversal traversal = new XmlTraversal();
                    traversal.Reader = xmlReader;
                    traversal.Traverse();
                }
            }
        }

        [TestMethod()]
        public void TraverseOutOfPlaceCollectionsNodeTest()
        {
            String xml =
                @"<XmlAdapterTestObject name=""myName"" class=""myClass"" number=""1234567"">
                    <InnerObject>MyInnerObject</InnerObject>
                    <InnerObjectWithProperties prop1=""prop1"" prop2=""prop2""></InnerObjectWithProperties>
                    
                        <SampleObject name=""name1"" data=""data1""/>
                        <SampleObject name=""name2"" data=""data2""/>
                        <SampleObject name=""name3"" data=""data3""/>
                        <SampleObject name=""name4"" data=""data4""/>
                        <SampleObject name=""name5"" data=""data5""/>
                    
                </XmlAdapterTestObject>";

            using (StringReader stringReader = new StringReader(xml))
            {
                using (XmlReader xmlReader = new XmlTextReader(stringReader))
                {
                    XmlTraversal traversal = new XmlTraversal();
                    traversal.Reader = xmlReader;
                    traversal.Traverse();
                }
            }
        }

        [TestMethod()]
        public void TraverseCollectionsNodeTest()
        {
            String xml =
                @"<ObjectWithArray>
                        <SampleObject name=""name1"" data=""data1""/>
                        <SampleObject name=""name2"" data=""data2""/>
                        <SampleObject name=""name3"" data=""data3""/>
                        <SampleObject name=""name4"" data=""data4""/>
                        <SampleObject name=""name5"" data=""data5""/>
                 </ObjectWithArray>";

            using (StringReader stringReader = new StringReader(xml))
            {
                using (XmlReader xmlReader = new XmlTextReader(stringReader))
                {
                    XmlTraversal traversal = new XmlTraversal();
                    traversal.Reader = xmlReader;
                    traversal.Traverse();
                }
            }
        }

    }
}
